package com.spark.tags;


import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class Lov extends ComboBox {
	
	public static final String DEFAULT_TEMPLATES = "lov.ftl";
	
	public static final String PROPERTITY_CONTEXT = "context";
	public static final String PROPERTITY_LOV_WIDTH = "lovWidth";
	public static final String PROPERTITY_LOV_HEIGHT = "lovHeight";
	public static final String PROPERTITY_PROCESS_DATA = "processData";
	public static final String PROPERTITY_INIT_POPUP = "initPopup";
	public static final String PROPERTITY_TRIGGER_TOOLTIP = "triggerTooltip";
	
	public static final String PROPERTITY_PLUS_TRIGGER = "plusTrigger";
	public static final String PROPERTITY_PLUS_TRIGGER_ICON = "plusTriggerIcon";
	public static final String PROPERTITY_PLUG_TRIGGER_TOOLTIP = "plusTriggerTooltip";
	public static final String PROPERTITY_PLUS_TRIGGER_HANDLER = "plusTriggerHandler";
	public static final String PROPERTITY_PLUS_TRIGGER_RENDERER = "plusTriggerRenderer";
	
	public String getTemplate() {
		return DEFAULT_TEMPLATES;
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		super.init(view, context);
		
		context.addJsonConfig(PROPERTITY_CONTEXT, context.getContextPath());
		context.addJsonConfig(PROPERTITY_LOV_WIDTH, new Integer(view.getInt(PROPERTITY_LOV_WIDTH,400)));
		context.addJsonConfig(PROPERTITY_LOV_HEIGHT, new Integer(view.getInt(PROPERTITY_LOV_HEIGHT,400)));
		String rder = view.getString(PROPERTITY_PLUS_TRIGGER_RENDERER);
		if(rder!=null) context.addJsonConfig(PROPERTITY_PLUS_TRIGGER_RENDERER, rder);
		String pth = view.getString(PROPERTITY_PLUS_TRIGGER_HANDLER);
		if(pth!=null) context.addJsonConfig(PROPERTITY_PLUS_TRIGGER_HANDLER, pth);
		
		
		boolean initPopup = view.getBoolean(PROPERTITY_INIT_POPUP, false);
		if(initPopup){
			context.put(PROPERTITY_INIT_POPUP, "<div class=\"s_popup s_cb_v\" stype=\"popup\"></div>");
		}
		boolean pt = view.getBoolean(PROPERTITY_PLUS_TRIGGER, false);
		if(pt){
			Integer w = (Integer)context.get(PROPERTITY_INPUTWIDTH);
			context.put(PROPERTITY_INPUTWIDTH, w-22);
			String plusTriggerTooltip = view.getString(PROPERTITY_PLUG_TRIGGER_TOOLTIP, "");
			if(!"".equals(plusTriggerTooltip)){
				plusTriggerTooltip = "data-tooltip-type=\"tip\" data-tooltip=\""+plusTriggerTooltip+"\"";
			}
			String plustriggerIcon = view.getString(PROPERTITY_PLUS_TRIGGER_ICON,"");
			context.put(PROPERTITY_PLUS_TRIGGER, "<div class=\"s_tg "+plustriggerIcon+"\" "+plusTriggerTooltip+" style=\"display:none;height:"+context.get(PROPERTITY_TRIGER_HEIGHT)+"px\" stype=\"plusTrigger\" hideFocus></div>");
		}
		String triggerTooltip = view.getString(PROPERTITY_TRIGGER_TOOLTIP, "");
		if(!"".equals(triggerTooltip)){
			context.put(PROPERTITY_TRIGGER_TOOLTIP, "data-tooltip-type=\"tip\" data-tooltip=\""+triggerTooltip+"\"");
		}
		String processData = view.getString(PROPERTITY_PROCESS_DATA,"");
		if(!"".equals(processData)) context.addJsonConfig(PROPERTITY_PROCESS_DATA, processData);
	}
	
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\">");
		sb.append("    <div>");
		sb.append("    	<input class=\"s_tf\" stype=\"input\" tabindex=\"").append(get(map,"tabIndex")).append("\" style=\"width: ").append(get(map,"inputWidth")).append("px;line-height:").append(get(map,"lineHeight")).append("px;height:").append(get(map,"inputHeight")).append("px;").append(get(map,"inputStyle")).append("\" ").append(get(map,"readOnly")).append(" value=\"").append(get(map,"value")).append("\" autocomplete=\"off\"/>");
		sb.append("    </div>	");
		sb.append("    ").append(get(map,"plusTrigger"));
		sb.append("    <div class=\"s_tg s_lbtn\" style=\"height:").append(get(map,"trigerHeight")).append("px\" stype=\"trigger\" hideFocus ").append(get(map,"triggerTooltip")).append("></div>");
		sb.append("    ").append(get(map,"initPopup"));
		sb.append("</div>");
		sb.append("<script>");
		sb.append("    new Spark.Lov(").append(get(map,"config")).append(");");
		sb.append("</script>   ");
		return sb.toString();
	}
}
